/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Kuchcik
 */
public class Recipes {
    static String[] selectedRow;
    static JFrame parent;
    public Recipes(JFrame frame){
     this.parent=frame;
    }
    
    public static void addRecipe(int visitID,String content)
    {
         try {
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "INSERT INTO recepty VALUES(?,?)",
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmt.setInt(1, visitID);
            stmt.setString(2, content);
            stmt.executeUpdate();
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
    }
    public static JTable getRecipesByPesel(String pesel){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select R.id as ID, H.data as Data_wystawienia, R.opis as Szczegóły from recepty as R "
                    + "join historia_wizyt as H on R.id_wizyta=H.id "
                    + "join pacjenci as P on H.id_pacjenta=P.id_user where P.pesel = '"+pesel+"'";
            
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static JTable getRecipesBySurname(String surname){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select R.id as ID, H.data as Data_wystawienia, R.opis as Szczegóły from recepty as R "
                    + "join historia_wizyt as H on R.id_wizyta=H.id"
                    + " join users as U on H.id_pacjenta=U.id where U.nazwisko = '"+surname+"'";
            
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }

    public static JTable getRecipesByDoctor(String doctor_id){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select R.id as ID, H.data as Data_wystawienia, R.opis as Szczegóły from recepty as R "
                    + "join historia_wizyt as H on R.id_wizyta=H.id"
                    + " join users as U on H.id_medyka = U.id where U.id = "+doctor_id;
            
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }    
    
    public static JTable getRecipesListByWizytaID(int wizytaID){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select id AS 'Numer recepty' FROM recepty WHERE id_wizyta='"+wizytaID+"'";
            
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }   
    
    public static JTable getRecipesOListByWizytaID(int wizytaID){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select id AS 'Numer recepty', opis AS Opis FROM recepty WHERE id_wizyta='"+wizytaID+"'";
            
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static String getOpisById(int idpa) throws Exception{
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("SELECT opis FROM recepty WHERE id = '"+idpa+"'");
            ResultSet rs = stmn.getResultSet();
            rs.first();
            return rs.getString(1);         
        } catch (SQLException ex ) {
            System.out.println("Recipes.getOpisById - blad z baza danych");
          // ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
    return null;
    }
        
    public static void setSelectedRow(String[] row){
        Vaccine.selectedRow=row;
    }
    
    public static  String[] getSelectedRow(){
        return Vaccine.selectedRow;
    }

}
